package net.realsee.service.wcs.data.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.data.WcsCharact;
import net.realsee.domain.wcs.data.bo.WcsCharactBo;
import net.realsee.domain.wcs.data.vo.WcsCharactVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.data.WcsCharactMapper;
import net.realsee.service.wcs.data.IWcsCharactService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 特性Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsCharactServiceImpl extends ServiceImpl<WcsCharactMapper, WcsCharact> implements IWcsCharactService {

    private final WcsCharactMapper baseMapper;

    /**
     * 查询特性
     */
    @Override
    public WcsCharactVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询特性列表
     */
    @Override
    public TableDataInfo<WcsCharactVo> selectVoPage(WcsCharactBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsCharact> lqw = buildVoQueryWrapper(bo);
        Page<WcsCharactVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询特性列表
     */
    @Override
    public List<WcsCharactVo> selectVoList(WcsCharactBo bo) {
        LambdaQueryWrapper<WcsCharact> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsCharact> buildVoQueryWrapper(WcsCharactBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsCharact> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsCharact::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getCharact()), WcsCharact::getCharact, bo.getCharact());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), WcsCharact::getRemark, bo.getRemark());
        lqw.eq(StringUtils.isNotBlank(bo.getVersion()), WcsCharact::getVersion, bo.getVersion());
        lqw.eq(StringUtils.isNotBlank(bo.getCharactType()), WcsCharact::getCharactType, bo.getCharactType());
        lqw.eq(StringUtils.isNotBlank(bo.getCharactClass()), WcsCharact::getCharactClass, bo.getCharactClass());
        lqw.eq(bo.getUpperLimit() != null, WcsCharact::getUpperLimit, bo.getUpperLimit());
        lqw.eq(bo.getLowerLimit() != null, WcsCharact::getLowerLimit, bo.getLowerLimit());
        lqw.eq(bo.getTargetValue() != null, WcsCharact::getTargetValue, bo.getTargetValue());
        lqw.eq(StringUtils.isNotBlank(bo.getAttribute()), WcsCharact::getAttribute, bo.getAttribute());
        lqw.eq(bo.getRequired() != null, WcsCharact::getRequired, bo.getRequired());
        lqw.eq(bo.getControlUpperLimit() != null, WcsCharact::getControlUpperLimit, bo.getControlUpperLimit());
        lqw.eq(bo.getControlLowerLimit() != null, WcsCharact::getControlLowerLimit, bo.getControlLowerLimit());
        lqw.eq(StringUtils.isNotBlank(bo.getUomCode()), WcsCharact::getUomCode, bo.getUomCode());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsCharact::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsCharact::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsCharact::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsCharact::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsCharact::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsCharact::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsCharact::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsCharact::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsCharact::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsCharact::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsCharact::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsCharact::getRowVersionStamp, bo.getRowVersionStamp());
        return lqw;
    }

    /**
     * 新增特性
     */
    @Override
    public Boolean insertByBo(WcsCharactBo bo) {
        WcsCharact add = BeanUtil.toBean(bo, WcsCharact.class);
        return this.save(add);
    }

    /**
     * 修改特性
     */
    @Override
    public Boolean updateByBo(WcsCharactBo bo) {
        WcsCharact update = BeanUtil.toBean(bo, WcsCharact.class);
        return this.updateById(update);
    }

    /**
     * 批量删除特性
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
